//
//  LKHomeTableViewCell.swift
//  新浪微博
//
//  Created by 胡不归是大虾 on 16/9/27.
//  Copyright © 2016年 胡不归是大虾. All rights reserved.
//

import UIKit
import SDWebImage
//图片之间的间距
private let pictureCellMargin: CGFloat = 3
//计算图片的宽度
private let maxWidth = ScreenWidth - 2 * commonMargin
private let itemWidth = (maxWidth - 2 * pictureCellMargin) / 3
class LKStatusCell: UITableViewCell {
    //MARK:子控件属性
    ///微博发送者头像
    @IBOutlet weak var iconImageView: UIImageView!
    ///认证图标
    @IBOutlet weak var avatarImage: UIImageView!
    ///微博用户名
    @IBOutlet weak var posterNameLable: UILabel!
    //vip图标
    @IBOutlet weak var vipImage: UIImageView!
    ///微博发送时间
    @IBOutlet weak var postTimeLable: UILabel!
    ///来源
    @IBOutlet weak var sourceLable: UILabel!
    ///微博正文
    @IBOutlet weak var contentLable: UILabel!
    ///微博图片
    @IBOutlet weak var pictureView: LKPictureView!
    //MARK:底部工具栏属性
    @IBOutlet weak var bottonToolBar: UIView!
    ///评论按钮
    @IBOutlet weak var commentBtn: UIButton!
    ///转发按钮
    @IBOutlet weak var repostBtn: UIButton!
    ///点赞按钮
    @IBOutlet weak var likeBtn: UIButton!
    //MARK:约束属性
    ///配图模块宽度约束
    @IBOutlet weak var pWidth: NSLayoutConstraint!
    ///配图模块高度约束
    @IBOutlet weak var pHeight: NSLayoutConstraint!
    ///配图模块布局属性
    @IBOutlet weak var pictureViewLayout: UICollectionViewFlowLayout!
    @IBOutlet weak var picViewTopCons: NSLayoutConstraint!
    //MARK:转发视图属性
    
    @IBOutlet weak var repostContentLable: UILabel!
    //MARK:模型数据加载,数据绑定
    var viewModel : LKStatusViewModel?{
        didSet{
            iconImageView.sd_setImage(with: viewModel?.iconURL)
            posterNameLable.text = viewModel?.status?.user?.name
//            postTimeLable.text = viewModel?.status?.created_at
            postTimeLable.text = viewModel?.timeText
            sourceLable.text = viewModel?.sourceText
            contentLable.text = viewModel?.status?.text
            vipImage.image = viewModel?.vipImage
            avatarImage.image = viewModel?.avatarImage
//            commentBtn.setTitle( "\"(viewModel?.status?.comments_count)", for: .normal)
            //cell的直接属性应该都写在viewModel中,绑定底部工具栏的数据
            commentBtn.setTitle(viewModel?.comment_text, for: .normal)
            repostBtn.setTitle(viewModel?.repost_text, for: .normal)
            likeBtn.setTitle(viewModel?.like_text, for: .normal)
            //根据图片数组的数量计算collectionView的尺寸
            let count = viewModel?.pic_urls?.count ?? 0
            let pictureViewSize = changePictureViewSize(count: count)
            //更新约束
            pWidth.constant = pictureViewSize.picSize.width
            pHeight.constant = pictureViewSize.picSize.height
            pictureViewLayout.itemSize = pictureViewSize.itemSize
            picViewTopCons.constant = (count == 0 ? 0 : commonMargin)
            pictureView.pic_urls = viewModel?.pic_urls
            //转发微博正文数据绑定
            repostContentLable?.text = viewModel?.repostContentText
        }
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        self.contentLable.preferredMaxLayoutWidth = ScreenWidth - 2 * commonMargin
        self.repostContentLable?.preferredMaxLayoutWidth = ScreenWidth - 2 * commonMargin
        self.selectionStyle = .none
//        pictureViewLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        pictureViewLayout.minimumLineSpacing = pictureCellMargin
        pictureViewLayout.minimumInteritemSpacing = pictureCellMargin
    }
    //计算配图模块大小
    private func changePictureViewSize (count : Int ) -> (picSize:CGSize,itemSize:CGSize){
//        var pictureViewSize : CGSize
//        var itemSize : CGSize
        switch count {
        case 0:
            return (CGSize.zero, CGSize.zero)
        case 1:
            let urlString = viewModel?.pic_urls?.first?.wap_pic ?? ""
            //从缓存中拿图片
            let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: urlString)
            //指定默认的图片大小
            var imageSize = CGSize(width: 100, height: 120)
            if image != nil {
                imageSize = image!.size
            }
            return (imageSize,imageSize)
        case 4:
            let width = itemWidth * 2 + pictureCellMargin
//            pictureViewSize = CGSize(width: width, height: width)
            return (CGSize(width: width, height: width),CGSize(width: itemWidth, height: itemWidth))

        default:
            let row = CGFloat((count - 1) / 3 + 1)
            let height = itemWidth * row + (row - 1) * pictureCellMargin
//            pictureViewSize = CGSize(width: maxWidth, height: height)
            return (CGSize(width: maxWidth, height: height), CGSize(width: itemWidth, height: itemWidth))
        }
//        return pictureViewSize
    }
    func getRowHeight(viewModel: LKStatusViewModel) -> CGFloat {
        //给cell 的属性赋值,才能计算frame,并且要提前刷新布局,否则只有被显示的时候才能布局
        self.viewModel = viewModel
        //刷新布局
        self.contentView.layoutIfNeeded()
        //得到底部工具栏的最大Y
        return bottonToolBar.frame.maxY
    }
}
